<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Consume messages from a queue</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="amqpqueue.construct.html">AMQPQueue::__construct</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="amqpqueue.declare.html">AMQPQueue::declare</a></div>
 <div class="up"><a href="class.amqpqueue.html">AMQPQueue</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="amqpqueue.consume" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">AMQPQueue::consume</h1>
  <p class="verinfo">(PECL amqp &gt;= Unknown)</p><p class="refpurpose"><span class="refname">AMQPQueue::consume</span> &mdash; <span class="dc-title">Consume messages from a queue</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-amqpqueue.consume-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><span class="type void">void</span></span> <span class="methodname"><strong>AMQPQueue::consume</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = <strong><code>AMQP_NOPARAM</code></strong></span></span>
  ] )</div>

  <p class="para rdfs-comment">
   Blocking function that will retrieve the next message from the queue as it becomes available and will pass it off to the callback.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-amqpqueue.consume-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

     
      <dt>
<em><code class="parameter">callback</code></em></dt>

      <dd>

       <p class="para">
        A callback function to which the consumed message will be passed. The function must accept at a minimum one parameter, an <a href="class.amqpenvelope.html" class="classname">AMQPEnvelope</a> object,
        and an optional second parameter the <a href="class.amqpqueue.html" class="classname">AMQPQueue</a> from which the message was consumed.
       </p>
       <p class="para">
         The <span class="methodname"><strong>AMQPQueue::consume()</strong></span> will not return the processing thread back to the PHP script until the callback function returns <strong><code>FALSE</code></strong>.
       </p>
      </dd>

     
    
     <dt>
<em><code class="parameter">flags</code></em></dt>

     <dd>

      <p class="para">
       A bitmask of any of the flags: <strong><code>AMQP_NOACK</code></strong>.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-amqpqueue.consume-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   Throws <strong class="classname">AMQPChannelException</strong> if the channel is not open.
  </p>
  <p class="para">
   Throws <strong class="classname">AMQPConnectionException</strong> if the connection to the broker was lost.
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-amqpqueue.consume-returnvalues">
  <h3 class="title">返回值</h3>
 </div>


 <div class="refsect1 examples" id="refsect1-amqpqueue.consume-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4111">
    <p><strong>Example #1 <span class="methodname"><strong>AMQPQueue::consume()</strong></span> example</strong></p>
    <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Create&nbsp;a&nbsp;connection&nbsp;using&nbsp;all&nbsp;default&nbsp;credentials:&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">AMQPConnection</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$connection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect</span><span style="color: #007700">();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$channel&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">AMQPChannel</span><span style="color: #007700">(</span><span style="color: #0000BB">$connection</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;create&nbsp;a&nbsp;queue&nbsp;object&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$queue&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">AMQPQueue</span><span style="color: #007700">(</span><span style="color: #0000BB">$channel</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//declare&nbsp;the&nbsp;queue<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$queue</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">declare</span><span style="color: #007700">(</span><span style="color: #DD0000">'myqueue'</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">processMessage</span><span style="color: #007700">(</span><span style="color: #0000BB">$envelope</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$queue</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;global&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Message&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #DD0000">:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$envelope</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getBody</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Bail&nbsp;after&nbsp;10&nbsp;messages<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Consume&nbsp;messages&nbsp;on&nbsp;queue<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$queue</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">consume</span><span style="color: #007700">(</span><span style="color: #DD0000">"processMessage"</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="amqpqueue.construct.html">AMQPQueue::__construct</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="amqpqueue.declare.html">AMQPQueue::declare</a></div>
 <div class="up"><a href="class.amqpqueue.html">AMQPQueue</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
